package com.work.blog.controller;

import com.work.blog.constant.Constant;
import com.work.blog.model.BlogInfo;
import com.work.blog.service.BlogService;
import com.work.blog.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2025-06-16
 * Time:18:47
 */
@RequestMapping("/blog")
@RestController
@Slf4j
public class BlogController {
    @Autowired
    private BlogService blogService;

    //获取博客列表
    @RequestMapping(value = "/getList",method = RequestMethod.GET)
    public List<BlogInfo> getList(){
        log.info("获取博客列表..");
        return blogService.getList();
    }

    //获取博客详情
    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId,HttpServletRequest request){
        log.info("getBlogDetail,blogId: {}",blogId);
        BlogInfo blogDetail = blogService.getBlogDetail(blogId);
        //获取token
        String token = request.getHeader(Constant.REQUEST_HEADER_TOKEN);//拿到token
        //从token中获取登录用户的ID
        Integer userId = JwtUtils.getIdByToken(token);
        //判断作者是否为登录用户
        if(userId!=null && userId==blogDetail.getUserId()){
            blogDetail.setLoginUser(true);//为登录用户
        }else {
            blogDetail.setLoginUser(false);
        }
        return blogDetail;
    }

    //编辑发布博客
    @RequestMapping("/add")//用JSON去接收
    public Boolean addBook(@RequestBody BlogInfo blogInfo, HttpServletRequest request){
        log.info("添加图书， blogInfo:{}",blogInfo);
        //参数校验，判断其是否为空
        if (!StringUtils.hasLength(blogInfo.getTitle())||!StringUtils.hasLength(blogInfo.getContent())){
            //为空，返回false
            return false;
        }
        //也要获取登录用户，因为是登录用户发的博客userid
        //获取token
        String token = request.getHeader(Constant.REQUEST_HEADER_TOKEN);//拿到token
        //从token中获取登录用户的ID
        Integer userId = JwtUtils.getIdByToken(token);
        //若没有登录用户
        if (userId==null || userId<=0){
            return false;
        }
        //把用户ID添加进去
        blogInfo.setUserId(userId);
        return blogService.insertBook(blogInfo);
    }

    //更新博客
    @RequestMapping("/update")
    public Boolean update(BlogInfo blogInfo){
        log.info("更新博客，blogId:{}",blogInfo);
        //参数校验
        if (blogInfo.getId()==null||
                !StringUtils.hasLength(blogInfo.getTitle())||
                !StringUtils.hasLength(blogInfo.getContent())){
            return false;
        }
        blogService.update(blogInfo);
        return true;
    }

    //删除博客
    @RequestMapping("/delete")
    public Boolean delete(Integer blogId){
        log.info("删除博客，blogId:{}",blogId);
        if (blogId<=0){
            return false;
        }
        BlogInfo blogInfo=new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        blogService.update(blogInfo);
        return true;
    }

}
